package net.hackbard.piratenradio;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;


public class Radio extends Activity {

	private ImageButton playButton;

	private ImageButton stopButton;

	private TextView textStreamed;

	private static final int STOPSPLASH = 0;
	// time in milliseconds
	private static final long SPLASHTIME = 3000;

	private PlayerService mBoundService;

	private ImageView splash;

	private Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STOPSPLASH:
				splash.setVisibility(View.GONE);
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		initControls();
		bindService(new Intent(Radio.this, PlayerService.class), mConnection,
				Context.BIND_AUTO_CREATE);

		splash = (ImageView) findViewById(R.id.splashscreen);
		Message msg = new Message();
		msg.what = STOPSPLASH;
		myHandler.sendMessageDelayed(msg, SPLASHTIME);

	}

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			mBoundService = ((PlayerService.LocalBinder) service).getService();
		}

		public void onServiceDisconnected(ComponentName className) {
			mBoundService = null;
		}
	};

	private void initControls() {
		playButton = (ImageButton) findViewById(R.id.button_play);
		stopButton = (ImageButton) findViewById(R.id.button_stop);
		Button homepage = (Button) findViewById(R.id.Homepage);
		
		textStreamed = (TextView) findViewById(R.id.text_kb_streamed);
		playButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				
				String streamUrl = "http://media.piraten-radio.net:8001";
				RadioButton Mp3Button = (RadioButton) findViewById(R.id.optionMP3);
				RadioButton AacButton = (RadioButton) findViewById(R.id.optionAAC);

				if (Mp3Button.isChecked() == true) {
					streamUrl = "http://media.piraten-radio.net:8000/mp3-high-stream";
				}
				if (AacButton.isChecked() == true) {
					streamUrl = "http://media.piraten-radio.net:8000/aacplus_high_stream";
				}
					stopButton.setEnabled(true);
					playButton.setEnabled(true);
					mBoundService.startStreamingAudio(textStreamed, streamUrl);
			}
		});
		stopButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
					mBoundService.stopStreamingAudio();
					playButton.setEnabled(true);
					stopButton.setEnabled(false);
			}
		});
		playButton.setEnabled(true);
		stopButton.setEnabled(true);

		homepage.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Uri uri = Uri.parse("http://piraten-radio.net");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});

	}
}
